package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.AssessCarInfoDto;

public class AssessCarInfoDaoImp extends TmsDao implements AssessCarInfoDao {

	protected AssessCarInfoDaoImp() throws TmsDaoException {
		super();

	}

	/*
	 * （非 Javadoc）
	 * 
	 * @see com.tianqu.tms.dao.AssessCarInfoDao#getAssessCar(java.lang.String)
	 */
	@Override
	public AssessCarInfoDto getAssessCar(String serial) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		AssessCarInfoDto dto = null;
		try {
			StringBuffer sql = new StringBuffer("  SELECT	");
			sql.append(" * ");
			sql.append(" FROM ");
			sql.append(TmsTableNames.TMS_ASSESS_CAR_INFO);
			sql.append(" WHERE   ");
			sql.append(" SERIAL=? ");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, serial);
			rs = ps.executeQuery();
			while (rs.next()) {
				dto = new AssessCarInfoDto();
				dto.setId(rs.getInt("ID"));
				dto.setSerial(rs.getString("SERIAL"));
				dto.setLicensePlate(rs.getString("LICENSEPLATE"));
				dto.setVin(rs.getString("VIN"));
				dto.setVendorCode(rs.getString("VENDORCODE"));
				dto.setModelCode(rs.getString("MODELCODE"));
				dto.setCarType(rs.getInt("CARTYPE"));
				dto.setColor(rs.getInt("COLOR"));
				dto.setCarUse(rs.getInt("CARUSE"));
				dto.setRegDate(rs.getString("REGDATE"));
				dto.setSendCardDate(rs.getString("SENDCARDDATE"));
				dto.setAssessPirce(rs.getInt("ASSESSPIRCE"));
				dto.setOwnerType(rs.getInt("OWNERTYPE"));
				dto.setCarOwner(rs.getString("CAROWNER"));
				dto.setCarOwnerIdCardNo(rs.getString("CAROWNERIDCARDNO"));
				dto.setOwnerOrgNo(rs.getString("OWNERORGNO"));
				dto.setOwnerPhone(rs.getString("OWNERPHONE"));
				dto.setOwnerAddress(rs.getString("OWNERADDRESS"));
				dto.setInsertUserId(rs.getInt("INSERTUSERID"));
				dto.setInputTime(rs.getTimestamp("INPUTTIME"));
				dto.setUpdateTime(rs.getTimestamp("UPDATETIME"));
				dto.setUpdateUserId(rs.getInt("UPDATEUSERID"));
			}
			return dto;
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}

	}
    
	/**
	 * 插入评估信息
	 */
	@Override
	public void saveAssessCar(AssessCarInfoDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("  INSERT INTO	");
			sql.append(TmsTableNames.TMS_ASSESS_CAR_INFO);
			sql.append(" ( ");
			sql.append(" SERIAL, ");
			sql.append(" LICENSEPLATE, ");
			sql.append(" VIN, ");
			sql.append(" VENDORCODE,	 ");
			sql.append(" MODELCODE, ");
			sql.append(" CARTYPE, ");
			sql.append(" COLOR, ");
			sql.append(" CARUSE, ");
			sql.append(" REGDATE, ");
			sql.append(" SENDCARDDATE, ");
			sql.append(" ASSESSPIRCE, ");
			sql.append(" OWNERTYPE, ");
			sql.append(" CAROWNER, ");
			sql.append(" CAROWNERIDCARDNO, ");
			sql.append(" OWNERORGNO, ");
			sql.append(" OWNERPHONE, ");
			sql.append(" OWNERADDRESS, ");
			sql.append(" INSERTUSERID, ");
			sql.append(" UPDATEUSERID, ");
			
			sql.append(" INPUTTIME, ");
			sql.append(" UPDATETIME ");
			sql.append(" ) VALUES ( ");
			sql.append(" ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,SYSDATE(),SYSDATE() ");
			sql.append("  )");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, dto.getSerial());
			ps.setString(2, dto.getLicensePlate());
			ps.setString(3, dto.getVin());
			ps.setString(4, dto.getVendorCode());
			ps.setString(5, dto.getModelCode());
			ps.setInt(6, dto.getCarType());
			ps.setInt(7, dto.getColor());
			ps.setInt(8, dto.getCarUse());
			ps.setString(9, dto.getRegDate());
			ps.setString(10, dto.getSendCardDate());
			ps.setInt(11, dto.getAssessPirce());
			ps.setInt(12, dto.getOwnerType());
			ps.setString(13, dto.getCarOwner());
			ps.setString(14, dto.getCarOwnerIdCardNo());
			ps.setString(15, dto.getOwnerOrgNo());
			ps.setString(16, dto.getOwnerPhone());
			ps.setString(17, dto.getOwnerAddress());
			ps.setInt(18, dto.getInsertUserId());
			ps.setInt(19, dto.getUpdateUserId());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}

	}

	@Override
	public void updateAssessCar(AssessCarInfoDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("  UPDATE	");
			sql.append(TmsTableNames.TMS_ASSESS_CAR_INFO);
			sql.append(" SET ");
			sql.append(" LICENSEPLATE=?, ");
			sql.append(" VIN=?, ");
			sql.append(" VENDORCODE=?,	 ");
			sql.append(" MODELCODE=?, ");
			sql.append(" CARTYPE=?, ");
			sql.append(" COLOR=?, ");
			sql.append(" CARUSE=?, ");
			sql.append(" REGDATE=?, ");
			sql.append(" SENDCARDDATE=?, ");
			sql.append(" ASSESSPIRCE=?, ");
			sql.append(" OWNERTYPE=?, ");
			sql.append(" CAROWNER=?, ");
			sql.append(" CAROWNERIDCARDNO=?, ");
			sql.append(" OWNERORGNO=?, ");
			sql.append(" OWNERPHONE=?, ");
			sql.append(" OWNERADDRESS=?, ");
			sql.append(" UPDATEUSERID=?, ");
			sql.append(" UPDATETIME=SYSDATE() ");
			sql.append(" WHERE ");
			sql.append(" SERIAL=? ");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, dto.getLicensePlate());
			ps.setString(2, dto.getVin());
			ps.setString(3, dto.getVendorCode());
			ps.setString(4, dto.getModelCode());
			ps.setInt(5, dto.getCarType());
			ps.setInt(6, dto.getColor());
			ps.setInt(7, dto.getCarUse());
			ps.setString(8, dto.getRegDate());
			ps.setString(9, dto.getSendCardDate());
			ps.setInt(10, dto.getAssessPirce());
			ps.setInt(11, dto.getOwnerType());
			ps.setString(12, dto.getCarOwner());
			ps.setString(13, dto.getCarOwnerIdCardNo());
			ps.setString(14, dto.getOwnerOrgNo());
			ps.setString(15, dto.getOwnerPhone());
			ps.setString(16, dto.getOwnerAddress());
			ps.setInt(17, dto.getInsertUserId());
			ps.setString(18, dto.getSerial());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	
		
	}
}
